package io.renren.modules.com.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.com.entity.ComPaymentHistoryEntity;
import io.renren.modules.com.service.ComPaymentHistoryService;

/**
 * 
 * 
 * @author pfp
 * @email
 * @date 2017-07-22 13:11:39
 */
@RestController
@RequestMapping("compaymenthistory")
public class ComPaymentHistoryController {

	@Autowired
	private ComPaymentHistoryService comPaymentHistoryService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("compaymenthistory:list")
	public R list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<ComPaymentHistoryEntity> comPaymentHistoryList = comPaymentHistoryService.queryList(query);
		int total = comPaymentHistoryService.queryTotal(query);
		PageUtils pageUtil = new PageUtils(comPaymentHistoryList, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{serialNo}")
	@RequiresPermissions("compaymenthistory:info")
	public R info(@PathVariable("serialNo") Long serialNo) {
		ComPaymentHistoryEntity comPaymentHistory = comPaymentHistoryService.queryObject(serialNo);
		return R.ok().put("comPaymentHistory", comPaymentHistory);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("compaymenthistory:save")
	public R save(@RequestBody ComPaymentHistoryEntity comPaymentHistory) {
		ValidatorUtils.validateEntity(comPaymentHistory);
		return comPaymentHistoryService.save(comPaymentHistory);
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("compaymenthistory:update")
	public R update(@RequestBody ComPaymentHistoryEntity comPaymentHistory) {
		ValidatorUtils.validateEntity(comPaymentHistory);
		return comPaymentHistoryService.update(comPaymentHistory);
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("compaymenthistory:delete")
	public R delete(@RequestBody Long[] serialNos) {
		comPaymentHistoryService.deleteBatch(serialNos);
		return R.ok();
	}

	@RequestMapping("/export")
	public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) {
		try {
			response.setContentType("octets/stream");
			response.addHeader("Content-Disposition",
					"attachment;filename=" + new String("付款明细".getBytes("utf-8"), "iso8859-1") + ".xls");
			OutputStream out = response.getOutputStream();
			comPaymentHistoryService.export(out, params);
			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
